var s_main_id = getUrlParam("main_id");
var datas = {};
var index_array = [];

//初始化
function biz_start() {
	$("#datetime").text(new Date( +new Date() + 8 * 3600 * 1000 ).toJSON().substr(0,19).replace("T"," "));
	var data_param = {"param_name": "A01_TBINFO","session_id": session_id,"param_value1": s_main_id};
	get_ajax_baseurl(data_param,"A01_TBINFO");
}

function A01_TBINFO(input) {
	datas = input;
	//查询失败
    if (Call_QryResult(datas.A01_TBINFO) == false)
        return false;
   
	json_list = datas.A01_TBINFO;
	var t_single_table_list = datas.A01_TBINFO.t_single_table;
	$.each(t_single_table_list,function(i, obj) {
		var main_id = obj.MAIN_ID;
		var index_string = index_array.toString();
		if(index_array.indexOf(main_id) == -1){
			index_array.push(main_id);
		}
	});
	get_t_single_table_list();
	get_t_single_table_each();
	layer.close(ly_index);
}

//循环获取各个接口
function get_t_single_table_each(){
	let objTag = $("[name='T_INTERFACE']");
	let $div = $("[name='T_INTERFACE']");
	for(let i=0;i<index_array.length;i++){
		let NewO = document.createElement("div");
		$(NewO).html("<div name=\"T_INTERFACE_"+index_array[i]+"\">"+objTag.html()+"</div>");
		$div.before(NewO);
		get_t_single_table_detail(i,index_array[i]);
		get_t_table_column(i,index_array[i]);
		get_t_main_sub_link(i,index_array[i]);
	}
	$("[name='T_INTERFACE']").remove();
}

//接口列表
function get_t_single_table_list(){
	var msg = datas.A01_TBINFO.t_single_table;
	if($("[name='t_single_table_LIST']").length>0){ 
		let objTag = $("[name='t_single_table_LIST']");
		$.each(msg,function(i, obj) {
			var objTagNew = base_child_table(i,obj,"t_single_table_",objTag);
			var row_index = $(objTag)[0].rowIndex;
			var $tr = $(objTag).parent().find("tr").eq(row_index);
			$tr.before(objTagNew);
		});
		$(objTag).remove();
	}
}

//接口明细
function get_t_single_table_detail(index,proc_id){
	var t_single_table = datas.A01_TBINFO.t_single_table[index];
	tem_base_table(index,t_single_table,"t_single_table_DETAIL_",proc_id);
}

//表列名
function get_t_table_column(index,proc_id){
	var t_table_column = datas.A01_TBINFO.t_table_column;
	var msg = [];
	$.each(t_table_column,function(i, obj) {
		if(obj.TABLE_ID == proc_id){
			msg.push(obj);
		}
	});
	if($("[name=\"T_INTERFACE_"+proc_id+"\"]").find("[name='t_table_column']").length>0){
		let objTag = $("[name=\"T_INTERFACE_"+proc_id+"\"]").find("[name='t_table_column']");
		$.each(msg,function(i, obj) {
			let objTagNew = base_child_table(i,obj,"t_table_column_",objTag);
			let row_index = $(objTag)[0].rowIndex;
			let $tr = $(objTag).parent().find("tr").eq(row_index);
			$tr.before(objTagNew);
		});
		$(objTag).remove();
	}
}

//主从表
function get_t_main_sub_link(index,proc_id){
	var t_main_sub_link = datas.A01_TBINFO.t_main_sub_link;
	let msg = [];
	$.each(t_main_sub_link,function(i, obj) {
		if(obj.TABLE_ID == proc_id){
			msg.push(obj);
		}
	});
	if($("[name=\"T_INTERFACE_"+proc_id+"\"]").find("[name='t_main_sub_link']").length>0){
		let objTag = $("[name=\"T_INTERFACE_"+proc_id+"\"]").find("[name='t_main_sub_link']");
		$.each(msg,function(i, obj) {
			let objTagNew = base_child_table(i,obj,"t_main_sub_link_",objTag);
			let row_index = $(objTag)[0].rowIndex;
			let $tr = $(objTag).parent().find("tr").eq(row_index);
			$tr.before(objTagNew);
		});
		$(objTag).remove();
	}
}

/*回调函数*/
function Biz_special_fun(json_tag)
{
	//
	var o_text = json_tag[GetLowUpp("obj_value")];
	var iIndex = json_tag[GetLowUpp("iIndex")];
	var dt_name = json_tag[GetLowUpp("dt_name")];
	var return_name = json_tag[GetLowUpp("return_name")];
	var obj_value = json_tag[GetLowUpp("obj_value")];
	/**/
	if(dt_name == "t_single_table_" && return_name == "MAIN_ID")
		obj_value = iIndex +1;
	else if(dt_name == "t_table_column_" && return_name == "MAIN_ID")
		obj_value = iIndex +1;
	else if(dt_name == "t_main_sub_link_" && return_name == "MAIN_ID"){
		obj_value = iIndex +1;
	}
	else if(dt_name == "t_table_column_" && return_name == "COLUMN_TYPE"){
		if(obj_value.toString() == "1")
			obj_value = "VARCHAR";
		else if(obj_value.toString() == "2")
			obj_value = "INT";
		else if(obj_value.toString() == "3")
			obj_value = "FLOAT";
		else if(obj_value.toString() == "4")
			obj_value = "DATETIME";
		else
			obj_value = "VARCHAR";
	}
	/**/	
	return obj_value;
}

//加载html特殊函数
function Biz_special_html(json_tag){
	//
	var o_text = json_tag[GetLowUpp("obj_value")];
	var iIndex = json_tag[GetLowUpp("iIndex")];
	var dt_name = json_tag[GetLowUpp("dt_name")];
	var return_name = json_tag[GetLowUpp("return_name")];
	var obj_value = json_tag[GetLowUpp("obj_value")];	
	var input_data = json_list["B01_EVAWNF"];
	return "";
}

//循环基本表格数据填充
//i,msg,dt_name,objTag
function tem_base_table(index,msg,dt_name,proc_id){
	for(var obj in msg)
	{
		var o_text = s_decode(msg[obj]);
		if(dt_name == "t_single_table_DETAIL_" && obj == "MAIN_ID"){
			o_text = o_text.replace(s_decode(msg[obj]),(index+1)+".");
		}
		else if(dt_name == "t_table_column_" && obj == "MAIN_ID"){
			o_text = o_text.replace(s_decode(msg[obj]),(index+1)+".");
		}
		else if(dt_name == "t_main_sub_link_" && obj == "MAIN_ID"){
			o_text = o_text.replace(s_decode(msg[obj]),(index+1)+".");
		}
		$("#pagecontent").find("[name=\"T_INTERFACE_"+proc_id+"\"]").eq(0).find("[name='"+dt_name+obj+"']").find("span").text(o_text);
	}
}

$(document).ready(function () {
	init_page();
});

$("#export_word").click(function (event) {
	$("#pagecontent").wordExport(s_title+"数据结构文档"+dateFmt("yyyyMMddhh", new Date()) );
});

String.prototype.replaceAll = function(s1,s2){
　　return this.replace(new RegExp(s1,"gm"),s2);
}